import { ref } from 'vue'
import { adapter, request } from '@/request'
import { uploadBrandByPost } from '@/core/api/brand'
import { message } from 'ant-design-vue'
import type { RequestWrapper } from '@/core/interfaces/common'

export interface UploaderData {
  categoryId?: number
  info: string
  files?: Blob
}

export default function updateBrand() {
  const service = adapter<RequestWrapper<any>>(request)
  const loading = ref(false)
  const response = ref<RequestWrapper<any>>()

  const fetch = (data: UploaderData) => {
    const { url, method } = uploadBrandByPost()
    loading.value = true

    const formData = new FormData()
    Object.keys(data).forEach((key) => {
      // @ts-ignore-next-line
      formData.append(key, data[key])
    })

    return service({
      config: {
        url,
        method,
        data: formData
      },
      callback: (res) => {
        response.value = res.data
        message.success(res.data.msg)
      },
      done: () => {
        loading.value = false
      }
    })
  }

  return [response, fetch, loading] as const
}
